package longrise.phone.com.bjjt_jyb.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import longrise.phone.com.bjjt_jyb.R;
import longrise.phone.com.bjjt_jyb.Utils.AllCapTransformationMethod;
import longrise.phone.com.bjjt_jyb.Utils.UiUtil;
import longrise.phone.com.bjjt_jyb.View.AbstractSpinerAdapter;

/**
 * 填写信息的自定义组件。
 */
public class SetTextModule extends LinearLayout implements AbstractSpinerAdapter.IOnItemSelectListener {

    private final Context context;
    private int textAttr;
    private TextView tv_text2;
    private EditText et_text1;
    private RadioGroup rg_cartype;
    private RadioButton rb_cartype1;
    private RadioButton rb_cartype2;
    private RadioButton rb_cartype3;
    private TextView tv_select_province;
    private EditText et_text3;
    private CheckBox cb_yes;
    private TextView tv_gettext;
    private EditText et_text4;

    public SetTextModule(Context context) {
        this(context, null);
    }

    public SetTextModule(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SetTextModule(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SetTextModule);
        textAttr = ta.getInteger(R.styleable.SetTextModule_textAttr, 0);
        ta.recycle();
        this.context = context;
        init(context);
    }

    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.settextmoduleview, this);
        //选择车型
        LinearLayout ll_settextkind1 = (LinearLayout) view.findViewById(R.id.ll_settextkind1);
        rg_cartype = (RadioGroup) view.findViewById(R.id.rg_cartype);
        rb_cartype1 = (RadioButton) view.findViewById(R.id.rb_cartype1);
        rb_cartype2 = (RadioButton) view.findViewById(R.id.rb_cartype2);
        rb_cartype3 = (RadioButton) view.findViewById(R.id.rb_cartype3);
        //填写文字
        LinearLayout ll_settextkind2 = (LinearLayout) view.findViewById(R.id.ll_settextkind2);
        tv_text2 = (TextView) view.findViewById(R.id.tv_text2);
        et_text1 = (EditText) view.findViewById(R.id.et_text1);
        et_text1.setTransformationMethod(new AllCapTransformationMethod());//监测大写
        //选择省份选择
        LinearLayout ll_settextkind3 = (LinearLayout) view.findViewById(R.id.ll_settextkind3);
        tv_select_province = (TextView) view.findViewById(R.id.tv_select_province);
        et_text3 = (EditText) view.findViewById(R.id.et_text3);
        cb_yes = (CheckBox) view.findViewById(R.id.cb_yes);
        et_text3.setTransformationMethod(new AllCapTransformationMethod());
        //获取保单
        LinearLayout ll_settextkind4 = (LinearLayout) view.findViewById(R.id.ll_settextkind4);
        tv_gettext = (TextView) view.findViewById(R.id.tv_gettext);
        et_text4 = (EditText) view.findViewById(R.id.et_text4);
       /* 组件的种类，1代表选择车型，2代表填写文字，3代表车牌选择，4代表保单选择，5代表选择车型时两种车型 */
        switch (textAttr) {
            case 1:
                ll_settextkind2.setVisibility(View.GONE);
                ll_settextkind3.setVisibility(View.GONE);
                ll_settextkind4.setVisibility(View.GONE);
                rb_cartype1.setText(" 两轮车");
                rb_cartype2.setText(" 三轮车");
                rb_cartype3.setText(" 四轮车");
                break;
            case 2:
                ll_settextkind1.setVisibility(View.GONE);
                ll_settextkind3.setVisibility(View.GONE);
                ll_settextkind4.setVisibility(View.GONE);
                break;
            case 3:
                ll_settextkind1.setVisibility(View.GONE);
                ll_settextkind2.setVisibility(View.GONE);
                ll_settextkind4.setVisibility(View.GONE);
                initProvinceList();
                break;
            case 4:
                ll_settextkind1.setVisibility(View.GONE);
                ll_settextkind2.setVisibility(View.GONE);
                ll_settextkind3.setVisibility(View.GONE);
                break;
            case 5:
                ll_settextkind2.setVisibility(View.GONE);
                ll_settextkind3.setVisibility(View.GONE);
                ll_settextkind4.setVisibility(View.GONE);
                rb_cartype1.setText(" 小型汽车");
                rb_cartype2.setText(" 大型汽车");
                rb_cartype3.setVisibility(View.GONE);
                break;
        }

    }

    /********************************** 选择车型相关方法 ***************************************/
    public RadioGroup getRadioGroup() {
        return rg_cartype;
    }

    public int getRadioButtonOneId() {
        return rb_cartype1.getId();
    }

    public int getRadioButtonTwoId() {
        return rb_cartype2.getId();
    }

    public int getRadioButtonThreeId() {
        return rb_cartype3.getId();
    }

    /********************************** 填写文字相关方法 ***************************************/
    /**
     * 初始化模型左边的文字
     *
     * @param text
     */
    public void setTextModleParameter(String text) {
        tv_text2.setText(text);
        StringBuilder sb=new StringBuilder();
        sb.append("请输入");
        String substring = text.substring(0, text.length() - 1);
        sb.append(substring);
        et_text1.setHint(sb.toString());
    }

    /**
     * 填写editText
     */
    public void setText(String s) {
        et_text1.setText(s);
    }

    /**
     * 获取editText的文字
     */
    public String getTextModuleText() {
        String setText = et_text1.getText().toString().trim();
        return setText;
    }


    /********************************** 车牌号相关方法 ***************************************/

    /**
     * 弹出选择省份的弹框
     * @param
     */
    public void showPop() {
        UiUtil.showSelectPopWindow(context, provinceList, this,tv_select_province);
    }

    /**
     * 获取车牌的省份
     */
    public String getProvinceText() {
        String province = tv_select_province.getText().toString().trim();
        return province;
    }

    /**
     * 获取车牌前部分控件
     */
    public TextView getProvinceFirstView() {
        return tv_select_province;
    }

    /**
     * 获取车牌后部分控件
     */
    public EditText getProvinceNextView() {
        return et_text3;
    }

    /**
     * 获取车牌后部分内容
     */
    public String getProvinceNext() {
        String carNoNext = et_text3.getText().toString().trim().toUpperCase();
        return carNoNext;
    }

    /**
     * 获取整个车牌内容
     */
    public String getCarNo() {
        String provinceText = getProvinceText();
        String provinceNext = getProvinceNext();
        String carNo=provinceText+provinceNext;
        return carNo;
    }

    /**
     * 判断是否临牌
     * @return
     */
    public boolean isChecked(){
        return cb_yes.isChecked();
    }

    /************************************* 获取保单相关方法 ******************************************/


    /**
     * 拿到获取
     * @return
     */
    public TextView getGainTextView(){
        return tv_gettext;
    }

    /**
     * 拿到editText
     * @return
     */
    public EditText getGainEdittext(){
        return et_text4;
    }

    /**
     * 设置保单的内容
     * @return
     */
    public void setGainText(String s){
        et_text4.setText(s);
    }

    /**
     * 拿到保单的内容
     * @return
     */
    public String getGainText(){
        return et_text4.getText().toString().trim();
    }
    /************************************************************************************************/
    private List<String> provinceList = new ArrayList<>();

    private void initProvinceList() {
        String[] strs = {"豫", "京", "津", "冀", "晋", "蒙", "辽", "吉", "黑",
                "沪", "苏", "浙", "皖", "闽", "赣", "鲁", "鄂", "湘", "粤", "桂",
                "琼", "渝", "川", "贵", "云", "藏", "陕", "甘", "青", "宁", "新"};
        for (String str : strs) {
            provinceList.add(str);
        }
    }

    @Override
    public void onItemClick(int pos, View view) {
        String carProvince = provinceList.get(pos);
        tv_select_province.setText(carProvince);
    }
}
